﻿@section body {
    <form class="layui-form" action="">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form-item">
                    <input class="layui-hide" name="ID" value="@this.Context.Request.Query["userID"].FirstOrDefault()" />
                    <label class="layui-form-label">新密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password" placeholder="请输入6到12个字符" lay-verify="pass" lay-verType="tips" autocomplete="off" id="LAY_password" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">确认新密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="repassword" placeholder="请输入确认密码" lay-verify="repass" lay-verType="tips" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                            <i class="layui-icon layui-icon-ok"></i>
                            提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
}
@section script {

    <script>
        layui.use(['form', 'view', 'jquery'], function () {
            let form = layui.form;
            let view = layui.view;
            let $ = layui.jquery;

            form.verify({
                pass: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"]
                , repass: function (value, item) { //value：表单的值、item：表单的DOM对象
                    if (value !== $("#LAY_password").val())
                        return "两次密码输入不一致"
                }
            });

            //监听提交
            form.on('submit(user-save)', function (data) {
                //layer.alert(JSON.stringify(data.field), {
                //    title: '最终的提交信息'
                //})
                //请求登入接口
                view.req({
                    url: 'UserChangePasswordUpdate' //实际使用请改成服务端真实接口
                    , data: { userID: data.field.ID, password: trim(data.field.password), __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value }
                    , type: "post"
                    , dataType: "json"
                    //,contentType: 'application/json'
                    , done: function (res) {
                        if (res.codeIndex == 1) {
                            layer.msg(res.msg, {
                                offset: '15px'
                                , icon: 1
                                , time: 1000
                            });
                        } else {
                            layer.msg(res.msg, {
                                offset: 't',
                                anim: 6
                            });
                        }

                    }
                });

                return false;
            });
        })
        //遍历对象去值的空格
        function TrimObjToNewObj(obj) {
            for (var index in obj) {
                obj[index] = trim(obj[index]);
            }
            return obj;
        }
        function trim(str) { //删除左右两端的空格
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }
    </script>
}
